package cn.edu.buaa.cnsatm.traffic_big_data_server.highway.dao;

import cn.edu.buaa.cnsatm.traffic_big_data_server.util.JDBCUtil;
import org.apache.activemq.artemis.api.core.Pair;

import javax.xml.transform.Result;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ImageDAO {
    /**
     * @author: LX
     * @className: ImageDAO
     * @description: 查询，增加数据库中的云图、路天气图
     * @data: 2019-10-29
     **/
    public static ResultSet getLatestImage(String imageType){
        /**
         * @author: LX
         * @methodName: getLatestImage
         * @description: 查询数据库中的云图、路天气图
         * @data: 2019-10-29
         **/
        String table = new String(), time = new String();
        if(imageType == "cloud"){table = "cloud_image"; time = "cloud_time";}
        if(imageType == "road" ){table = "road_weather_image"; time = "road_time";}
        String sql = "SELECT image_name, update_time, " + time + " FROM " + table +
                " where " + time + " =  (select max(" + time + ") from " + table +")";
        Statement statement = null;
        ResultSet resultSet = null;
        try{
            statement = JDBCUtil.databaseConnecttion.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            //e.printStackTrace();
        }

        return resultSet;
    }

    public static int insertImage(String imageType, String image_dir, String image_name, String update_time, String image_time){
        /**
         * @author LX
         * @description 将爬下来的图插入到相应的表中
         * @return
         * @date 2019-10-29
         */
        int insertNum = 0;
        String table = new String(), time = new String();
        if(imageType == "cloud"){table = "cloud_image"; time = "cloud_time";}
        if(imageType == "road" ){table = "road_weather_image"; time = "road_time";}
        String sql = "insert into " + table + "(image_dir, image_name, update_time, " + time + ") values('" +
                image_dir + "', '" + image_name + "', '" + update_time + "', '" + image_time + "')";
        System.out.println(sql);
        Statement stmt = null;
        try {
            stmt = JDBCUtil.databaseConnecttion.createStatement();//创建数据库查询实体
            insertNum = stmt.executeUpdate(sql);//执行Sql查询语句
            //System.out.print(insertNum);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return insertNum;
    }
}
